package com.atguigu.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ManagerFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("dofilter方法");
        HttpServletRequest httpServletRequest=(HttpServletRequest)req;
        HttpSession session=httpServletRequest.getSession();

        Object user=session.getAttribute("user");
        if(user==null){//未登录,访问被代理到登录页面
            System.out.println("用户未登录");
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
            return;
        }else{
            System.out.println("用户已登录");
            chain.doFilter(req,resp);//放行 去下一个过滤器或下面的正常逻辑
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
